﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using NHibernate;
using NHibernate.Cfg;

namespace SundayWishBLL
{
    public class NHibernateHelper : IHttpModule
    {

        private const string sessionKey = "nhibernate.current_session";
        private static string path = HttpContext.Current.Server.MapPath("~/hibernate.cfg.xml");
        private static ISessionFactory sessionFactory=(new Configuration()).Configure(path).BuildSessionFactory();
        static readonly object padlock = new object();
        /// <summary>
        /// 初始化，建立只读的sessionfactory
        /// </summary>
        private static ISessionFactory BuildSessionFactory(string AssemblyName)
        {

            NHibernate.Cfg.Configuration cfg = new Configuration();

            cfg.AddAssembly(AssemblyName);

            return cfg.BuildSessionFactory();

        }
        /// <summary>
        /// 初始化操作，在会话开始请求和会话结束请求事件处理中加上自定义的Application_BeginRequest和Application_EndRequest事件
        /// </summary>
        /// <param name="context"></param>
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(Application_BeginRequest);
            context.EndRequest += new EventHandler(Application_EndRequest);
        }

        public void Dispose()
        {
        }
        /// <summary>
        /// 在一次会话请求开始的时候初始化当前的Httpcontext的session
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = sessionFactory.OpenSession();
            context.Items[sessionKey] = currentSession;
        }
        /// <summary>
        /// 在服务器端返回消息前，关闭当前httpcontext的session
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Application_EndRequest(object sender, EventArgs e)
        {

            CloseSession();
        }
        /// <summary>
        /// 获取当前请求会话请求
        /// </summary>
        /// <returns></returns>
        public static ISession GetSession()
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = (ISession)context.Items[sessionKey];
            if (currentSession == null)
            {
                currentSession = sessionFactory.OpenSession();
                context.Items[sessionKey] = currentSession;
            }
            return currentSession;
        }
        /// <summary>
        /// 统一的关闭session操作
        /// </summary>
        public static void CloseSession()
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = (ISession)context.Items[sessionKey];

            if (currentSession == null)
            {
                // No current session
                return;
            }
            currentSession.Flush();
            currentSession.Close();
            context.Items.Remove(sessionKey);
        }
    }
}
